
package com.hd2.controlhd.dao;

import com.hd2.controlhd.banco.acessoBD;
import com.hd2.controlhd.entidade.Coockie;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Dulcyelliton
 */
public class usuarioDAO {
    
    private static usuarioDAO instance;
    
     //Método responsável por logar os usuários do sistema
    public boolean logar(String login, String senha) throws SQLException {

        acessoBD bd = acessoBD.getInstance();
        
        if (login.trim().equals("") || senha.trim().equals("")) {
            return false;
        } else {
            bd.Conectar();
            PreparedStatement Stmt;
            ResultSet rs;

            String query = "SELECT login, senha, tipo, nome_usuario FROM usuarios WHERE login = '" + login.trim() + "'";

            Stmt = (PreparedStatement) bd.getConection().prepareStatement(query);

            rs = Stmt.executeQuery();
            
            while (rs.next()) {
                
                if (login.trim().equals(rs.getString("login"))) {
                    
                    if (senha.equals(rs.getString("senha"))) {
                        Coockie coockie = Coockie.getInstance();
                        coockie.setNome(rs.getString("nome_usuario"));
                        coockie.setTipo(rs.getInt("tipo"));
                        bd.Desconectar();
                        return true;
                    }
                    return false;
                }
            }
        }
        bd.Desconectar();
        return false;

    }
    
    
    public static usuarioDAO getInstance(){
        if(instance == null){
            instance = new usuarioDAO();
        }
        return instance;
    }
    
    public static void kilInstance(){
        instance = null;
    }
    
}
